﻿using System;
using System.Collections;
using System.Data;

namespace TextEditor
{
    

    public class SectionList
    {
        private ArrayList myList = new ArrayList();
        public string SelectSQL = "Select * From SectionTable Order By SectionName";

        public void Add(string SectionCode, string SectionName)
        {
            SectionItem item = new SectionItem();
            item.SectionCode = SectionCode;
            item.SectionName = SectionName;
            this.myList.Add(item);
        }

        public void Clear()
        {
            this.myList.Clear();
        }

        public bool Contains(string strSection)
        {
            foreach (SectionItem item in this.myList)
            {
                if (item.SectionCode == strSection)
                {
                    return true;
                }
            }
            return false;
        }

        public int FromReader(IDataReader myReader)
        {
            int num = 0;
            if (myReader.FieldCount < 2)
            {
                return -1;
            }
            if (myReader.FieldCount == 2)
            {
                while (myReader.Read())
                {
                    SectionItem item = new SectionItem();
                    item.SectionName = myReader.GetString(1);
                    item.SectionCode = myReader.GetString(0);
                    this.myList.Add(item);
                    num++;
                }
                return num;
            }
            int i = -1;
            int num3 = -1;
            for (int j = 0; j < myReader.FieldCount; j++)
            {
                switch (myReader.GetName(j).ToUpper().Trim())
                {
                    case "SECTIONNAME":
                        i = j;
                        break;

                    case "SECTION":
                    case "ZSECTION":
                        num3 = j;
                        break;
                }
            }
            if ((i == -1) || (num3 == -1))
            {
                return -1;
            }
            while (myReader.Read())
            {
                SectionItem item2 = new SectionItem();
                item2.SectionName = myReader.GetString(i);
                item2.SectionCode = myReader.GetString(num3);
                this.myList.Add(item2);
                num++;
            }
            return num;
        }

        public string GetSectionCode(int index)
        {
            SectionItem item = (SectionItem) this.myList[index];
            return item.SectionCode;
        }

        public string GetSectionCode(string strSectionName)
        {
            foreach (SectionItem item in this.myList)
            {
                if (item.SectionName == strSectionName)
                {
                    return item.SectionCode;
                }
            }
            return null;
        }

        public string GetSectionName(int index)
        {
            SectionItem item = (SectionItem) this.myList[index];
            return item.SectionName;
        }

        public string GetSectionName(string strSectionCode)
        {
            foreach (SectionItem item in this.myList)
            {
                if (item.SectionCode == strSectionCode)
                {
                    return item.SectionName;
                }
            }
            return null;
        }

        public string[] GetSectionNameArray()
        {
            string[] strArray = new string[this.myList.Count];
            for (int i = 0; i < this.myList.Count; i++)
            {
                strArray[i] = ((SectionItem) this.myList[i]).SectionName;
            }
            return strArray;
        }

        public int IndexOf(string strSectionCode)
        {
            for (int i = 0; i < this.myList.Count; i++)
            {
                if (((SectionItem) this.myList[i]).SectionCode == strSectionCode)
                {
                    return i;
                }
            }
            return -1;
        }

        public int Count
        {
            get
            {
                return this.myList.Count;
            }
        }

        protected class SectionItem
        {
            public string SectionCode;
            public string SectionName;
        }
    }
}

